<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="object.Message" %>
<%@ page import="Dao.MessageDaoImpt" %>
<%@ page import="jakarta.servlet.http.Cookie" %>
<html>
<head>
    <title>留言修改界面</title>
    <link rel="stylesheet" href="table.css">
    <!--检测是否非法访问-->
    <%
        Cookie cookie[] = request.getCookies();
        String uname = null;
        String uid = null;
        for (int i = 0; i < cookie.length; i++) {
            if (cookie[i].getName().equals("uname")) {
                uname = cookie[i].getValue();
            }
            if (cookie[i].getName().equals("uid")) {
                uid = cookie[i].getValue();
            }
        }
        if (uname == null || uname.equals("")) {
            out.println("<script>alert('非法访问，正在返回登录界面')</script>");
            response.setHeader("refresh", "0;URL=login.jsp");
        }
        if (uid == null || uid.equals("")) {
            out.println("<script>alert('非法访问，正在返回登录界面')</script>");
            response.setHeader("refresh", "0;URL=login.jsp");
        }
    %>
</head>
<body>
<table>
    <tbody>
    <tr>
        <td colspan="7"><h3>即将修改：</h3></td>
    </tr>
    <tr>
        <td>编号</td>
        <td>标题</td>
        <td>用户名</td>
        <td>内容</td>
    </tr>
    <%
        MessageDaoImpt Dao = new MessageDaoImpt();
        String mid = request.getParameter("mid");
        String ntitle = request.getParameter("update-title");
        String ncontent = request.getParameter("update-content");
        String type = request.getParameter("type");
        Message message = Dao.SelectMessage(Integer.parseInt(mid));
        Message nmessage = new Message(message.getMid(), ntitle, message.getAuthor(), ncontent);
        //判断是否为本人信息
        if (uname.equals(message.getAuthor())) {
            //新内容为空时说明第一次进入该页面，输出原信息内容
            if (ncontent == null) {
                //表单
                out.println("<form action=\"\" method=\"post\">\n" +
                        "<tr>\n" + "<td>" + message.getMid() + "</td>\n" +
                        "<td><input type=\"text\" name=\"update-title\" value=\"" + message.getTitle() + "\"></td>\n" +
                        "<td>" + message.getAuthor() + "</td>\n" +
                        "<td><input type=\"text\" name=\"update-content\" value=\"" + message.getContent() + "\"></td>\n" +
                        "<td><input type=\"submit\" value=\"修改\" name=\"type\"></td>\n" +
                        "<td><input type=\"submit\" value=\"返回\" name=\"type\"></td>\n" +
                        "</tr>\n" + "</form>");
                out.println("<tr><td></td></tr>");
            } else {
                if (type.equals("返回")) {
                    out.println("<script>alert('未发生修改，正在返回信息界面')</script>");
                } else {
                    //对比两个message有无变化（信息是否更改），无更改直接返回信息页面
                    if ((nmessage.getTitle().equals(message.getTitle()) && nmessage.getContent().equals(message.getContent()))) {
                        out.println("<script>alert('未发生修改，正在返回信息界面')</script>");
                    } else {
                        int num = Dao.UpdateMessage(nmessage);
                        switch (num) {
                            case -1:
                                out.println("<script>alert('未查找到信息')</script>");
                                break;
                            case -2:
                                out.println("<script>alert('出现问题，请联系管理员')</script>");
                                break;
                            default:
                                out.println("<script>alert('已修改" + num + "条')</script>");
                        }
                    }
                    response.setHeader("refresh", "0;URL=message.jsp");
                }
            }
        } else {
            out.println("<script>alert('无法修改非本人信息')</script>");
            response.setHeader("refresh", "0;URL=message.jsp");
        }
    %>
    </tbody>
</table>
</body>
</html>
